// pages/myAddr/myAddr.js
let util = require('../../utils/util.js')
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addrs:[]
  },
  toAddAddr(){
    wx.navigateTo({
      url:"../addAddr/addAddr"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  getAddr(){
    let that = this
    util.post({
      url: 'https://groomtea.icebartech.com/api/miniApp/dealer/shippingAddressList',
      success: res => {
        console.log(res);
        let Addrs = res.data.bussData;
        
        wx.request({
          url: "https://groomtea.icebartech.com/api/base/sys/mini/loadDictionary?codeType=province",
          header: { "Accept": "application/json;charset=UTF-8" },
          method: 'post',
          success(res) {
            console.log(res);
            let place = res.data.data.bussData;
            
            for (let i = 0; i < Addrs.length; i++) {
              Addrs[i].place = "";
              for (let j = 0; j < place.length; j++) {
                if (Addrs[i].provinceCode == place[j].key) {
                  console.log('got province')
                  Addrs[i].place += place[j].value;
                  for (let k = 0; k < place[j].childs.length; k++) {
                    if (Addrs[i].cityCode == place[j].childs[k].key) {
                      Addrs[i].place += place[j].childs[k].value
                      for (let l = 0; l < place[j].childs[k].childs.length; l++) {
                        if (Addrs[i].districtCode == place[j].childs[k].childs[l].key) {
                          console.log('I finder!');
                          Addrs[i].place += place[j].childs[k].childs[l].value
                        }
                      }
                    }
                  }
                }
              }
            }
            Addrs.map((item, index) => {
              item.fn = item.consignee.substr(0, 1);
              item.color = (parseInt(item.mobile.substr(9, 2)) % 3) + 1

              return item;
            })
            that.setData({
              addrs: Addrs
            })
          }
        })
      }
    })
  },

  chooseAddr(e){
    console.log(e.currentTarget.dataset.addr);
    let addr = e.currentTarget.dataset.addr.place + e.currentTarget.dataset.addr.detailAddress;
    let addressType = e.currentTarget.dataset.addr.addressType
    let consignee = e.currentTarget.dataset.addr.consignee
    let mobile = e.currentTarget.dataset.addr.mobile
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    if (prevPage.route == "pages/balance/balance"){
      prevPage.setData({
        addrInfo:{
          myAddr: addr,
          addressType: addressType,
          consignee: consignee,
          mobile: mobile,
        }
      })
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddr();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})